package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.OrdersDao;
import com.hqyj.pojo.Orders;
import com.hqyj.pojo.PageData;
import com.hqyj.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;

    @Override
    public int addOrder(Integer goodsId,Integer customerId,double price) {
        //使用日期来生成订单号
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddmmss");
        String num = simpleDateFormat.format(date);
        Orders orders = new Orders();
        orders.setGoodsId(goodsId);
        orders.setCustomerId(customerId);
        orders.setPrice(price);
        //状态默认为0,未付款
        orders.setStatus(0);
        //订单号随机生成
        orders.setOrderNumber(num);
        System.out.println(orders);
        int i = ordersDao.insertOrder(orders);
        return i;
    }

    //用户的订单查询
    @Override
    public List<Map<String, Object>> getUserOrder(String username) {
        List<Map<String, Object>> list = ordersDao.queryUserOrder(username);
        return list;
    }



    @Override
    public int setStatusById(Integer id,Integer status) {
        int i = ordersDao.updateOrderStatusById(id,status);
        System.out.println(i);
        return i;
    }



    @Override
    public List<Orders> getAllOders() {
        List<Orders> ordersList = ordersDao.queryOrdersAll();
        return ordersList;
    }

    @Override
    public List<Orders> getOrderByStatus(int status) {
        List<Orders> ordersList1 = ordersDao.queryOrderByStatus(status);
        return ordersList1;
    }

    @Override
    public PageData<Orders> getOrderByPage(int pageNumber, int pageSize) {
        //使用分页插件, 拦截下一次mybatis的查询
        PageHelper.startPage(pageNumber,pageSize);
        //查询全部用户信息
        List<Orders> userList = ordersDao.queryOrdersAll();
        //创建pageInfo，pageInfo会计算分页相关信息
        PageInfo<Orders> pageInfo = new PageInfo<>(userList);
        //创建我们自己的数据模型
        PageData<Orders> pageData = new PageData<>();

        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);
        //填充总页数数据
        pageData.setTotalPage(pageInfo.getPages());
        //填充总记录数
        pageData.setTotalSize((int)pageInfo.getTotal());

        if(pageInfo.isHasNextPage()){
            //有下一页，设置下一页的页码
            pageData.setNextPage(pageInfo.getNextPage());
        }else{
            //没有下一页，就是尾页
            pageData.setNextPage(pageInfo.getPages());
        }
        if(pageInfo.isHasPreviousPage()){
            //有上一页，设置上一页的页码
            pageData.setPreviousPage(pageInfo.getPrePage());
        }else{
            //没有上一页，设置为首页
            pageData.setPreviousPage(1);
        }
        //设置每页的用户记录信息
        pageData.setList(pageInfo.getList());

        return pageData;
    }

    @Override
    public int deleteOrder(int id) {
        int i = ordersDao.deleteOrderById(id);
        return i;
    }


}
